package com.example.administrator.trains;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class admin_order extends Activity {
    String vehicleNum;
    String startPlace;
    String endPlace;
    String startTime;
    String endTime;
    String date;
    String train_type;
    String type1_num;
    String type2_num;
    String type3_num;
    String type4_num;
    String type1_price;
    String type2_price;
    String type3_price;
    String type4_price;
    private String type1;
    private String type2;
    private String type3;
    private String type4;

    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                Toast.makeText(admin_order.this,"载入成功",Toast.LENGTH_LONG).show();
            }
            if(msg.what==2){
                Toast.makeText(admin_order.this,"载入失败",Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
        setContentView(R.layout.activity_admin_order);

        Intent i = getIntent();
        vehicleNum = i.getStringExtra("vehicleNum");
        startPlace = i.getStringExtra("startPlace");
        endPlace = i.getStringExtra("endPlace");
        startTime = i.getStringExtra("startTime");
        endTime = i.getStringExtra("endTime");
        date = i.getStringExtra("date");
        train_type = i.getStringExtra("type").trim();
        type1_num = i.getStringExtra("type1_num");
        type2_num = i.getStringExtra("type2_num");
        type3_num = i.getStringExtra("type3_num");
        type4_num = i.getStringExtra("type4_num");
        type1_price = i.getStringExtra("type1_price");
        type2_price = i.getStringExtra("type2_price");
        type3_price = i.getStringExtra("type3_price");
        type4_price = i.getStringExtra("type4_price");
        type1 = i.getStringExtra("type1");
        type2 = i.getStringExtra("type2");
        type3 = i.getStringExtra("type3");
        type4 = i.getStringExtra("type4");

        final TextView czh_tv1 = (TextView)findViewById(R.id.from_station);  //开始地
        final TextView czh_tv4 = (TextView)findViewById(R.id.to_station);  //目的地
        final TextView czh_tv2 = (TextView)findViewById(R.id.start_time);   //开始时间
        final TextView czh_tv16 = (TextView)findViewById(R.id.end_time); //到达时间
        final TextView czh_tv14 = (TextView)findViewById(R.id.train_num);  //车次
        final TextView czh_tv13 = (TextView)findViewById(R.id.date);  //日期
        final TextView czh_tv12 = (TextView)findViewById(R.id.ticket_price); //票价
        final TextView czh_tv15 = (TextView)findViewById(R.id.ticket_num);//票数

        TextView text_price1 = (TextView)findViewById(R.id.price1);
        TextView text_price2 = (TextView)findViewById(R.id.price2);
        TextView text_price3 = (TextView)findViewById(R.id.price3);
        TextView text_price4 = (TextView)findViewById(R.id.price4);
        TextView text_num1 = (TextView)findViewById(R.id.num1);
        TextView text_num2 = (TextView)findViewById(R.id.num2);
        TextView text_num3 = (TextView)findViewById(R.id.num3);
        TextView text_num4 = (TextView)findViewById(R.id.num4);
        TextView text_type1 = (TextView)findViewById(R.id.type1);
        TextView text_type2 = (TextView)findViewById(R.id.type2);
        TextView text_type3 = (TextView)findViewById(R.id.type3);
        TextView text_type4 = (TextView)findViewById(R.id.type4);

        text_price1.setText((type1_price + "元").trim());
        text_price2.setText((type2_price + "元").trim());
        text_price3.setText((type3_price + "元").trim());
        if (!type4_price.equals("无")) {
            text_price4.setText((type4_price + "元").trim());
        } else {
            text_price4.setText(type4_price.trim());
        }


        text_num1.setText((type1_num + "张").trim());
        text_num2.setText((type2_num + "张").trim());
        text_num3.setText((type3_num + "张").trim());
        if (!type4_num.equals("无")) {
            text_num4.setText((type4_num + "张").trim());
        } else {
            text_num4.setText(type4_num.trim());
        }

        text_type1.setText(type1);
        text_type2.setText(type2);
        text_type3.setText(type3);
        text_type4.setText(type4);
        Button tijiao = (Button)findViewById(R.id.bt_buy_ticket);

        czh_tv1.setText(startPlace.trim());
        czh_tv4.setText(endPlace.trim());
        czh_tv2.setText(startTime.trim());
        czh_tv16.setText(endTime.trim());
        czh_tv14.setText(vehicleNum.trim());
        czh_tv13.setText(date.trim());

        tijiao.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.setBackgroundDrawable(getResources().getDrawable(R.drawable.pswshape2));
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    v.setBackgroundDrawable(getResources().getDrawable(R.drawable.pswshape));

                    try {
                        //发送请求
                        Socket.connector.send(Socket.connector.package_data(Socket.connector.TICKET_CMD_TYPE,
                                Socket.connector.ADD_TICKET, "00",
                                vehicleNum.trim() + CommunicationData.empty +
                                        startPlace.trim() + CommunicationData.empty +
                                        endPlace.trim() + CommunicationData.empty +
                                        startTime.trim() + CommunicationData.empty +
                                        endTime.trim() + CommunicationData.empty +
                                        date.trim() + CommunicationData.empty +
                                        type1_num.trim() + CommunicationData.empty +
                                        type2_num.trim() + CommunicationData.empty +
                                        type3_num.trim() + CommunicationData.empty +
                                        type4_num.trim() + CommunicationData.empty +
                                        type1_price.trim() + CommunicationData.empty +
                                        type2_price.trim() + CommunicationData.empty +
                                        type3_price.trim() + CommunicationData.empty +
                                        type4_price.trim() + CommunicationData.empty));
                        Socket.connector.recv_ack();
                        if (CommunicationData.ack == "01") {
                            Socket.connector.recv_ack();
                            if (CommunicationData.ack == Socket.connector.ADD_TICKET_SUCCESS_ACK) {
                                //添加成功
                                Message m = new Message();
                                m.what = 1;
                                myHandler.sendMessage(m);
                            }
                            if (CommunicationData.ack == Socket.connector.ADD_TICKET_FALSE_ACK) {
                                //添加失败
                                Message m = new Message();
                                m.what = 2;
                                myHandler.sendMessage(m);
                            }
                        }
                    } catch (IOException e) {
                    }
                }
                return true;
            }
        });

    }
}
